<?php

/**
 * 后台关键字管理模块 Controller
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zwt007 $  <zwttmac@qq.com>
 * $Id: keywords.php 31 2011-07-11 15:25:51Z zwt007 $
 * http://www.t-mac.org；
 */
class keywordsAction extends Action
{

    private $tmp_model;

    /**
     * _init 方法 在执行任何Action前执行
     */
    public function _init()
    {
        $this->assign('action', $_GET['TMAC_ACTION']);
        $this->tmp_model = Tmac::model('keywords');
        $check_model = $this->M('check');
        $check_model->checkLogin();
        $check_model->CheckPurview('tb_admin');
    }

    /**
     * 关键字管理 首页
     */
    public function index()
    {
        //TODO  取出所有关键字
        $rs = $this->tmp_model->getKeywordsList();
        $this->V('keywords');
    }

    /**
     * 新增/修改关键字页面
     */
    public function add()
    {
        $k_id = (int) $_GET['k_id'];
        $editinfo = array();
        if ($k_id > 0) {
            $editinfo = $this->tmp_model->getKeywordsInfo($k_id);
        }
        $this->assign('editinfo', $editinfo);
        //TODO　载入资讯类别添加表单
        $this->V('keywords');
    }

    /**
     * 关键字管理 insert update => save()
     */
    public function save()
    {
        if (empty($_POST) || count($_POST) < 1) {
            $this->redirect('don\'t be evil');
            exit;
        }

        /* 初始化变量 */
        $k_id = !empty($_POST['k_id']) ? (int) $_POST['k_id'] : 0;
        $k_pagename = !empty($_POST['pagename']) ? trim($this->H($_POST['pagename'])) : '';
        $k_page = !empty($_POST['page']) ? trim($this->H($_POST['page'])) : '';
        $k_title = !empty($_POST['title']) ? trim($this->H($_POST['title'])) : '';
        $k_keywords = !empty($_POST['keywords']) ? trim($this->H($_POST['keywords'])) : '';
        $k_description = !empty($_POST['description']) ? trim($this->H($_POST['description'])) : '';
        $k_rule = !empty($_POST['rule']) ? trim($this->H($_POST['rule'])) : '';

        /* 验证不能为空 */
        if (empty($k_pagename)) {
            $this->redirect('页面名不能为空!');
            exit;
        }
        if (empty($k_keywords)) {
            $this->redirect('页面关键字不能为空');
            exit;
        }
        if (empty($k_description)) {
            $this->redirect('页面描述不能为空');
            exit;
        }
        // TODO goon to verify

        $info = array(
            'k_pagename' => $k_pagename,
            'k_title' => $k_title,
            'k_keywords' => $k_keywords,
            'k_description' => $k_description,
            'k_time' => time()
        );

        if ($k_id > 0) {
            $keywords_page = HttpResponse::getCookie('keywords_page');
            //update save article
            $where = 'k_id = ' . $k_id . '';
            $rs = $this->tmp_model->addKeywords($info, 'UPDATE', $where);
            if ($rs) {
                $this->redirect('修改关键字内容成功', PHP_SELF . '?m=keywords&page= ' . $keywords_page . '');
            } else {
                $this->redirect('修改关键字内容失败');
            }
        } else {
            $info['k_page'] = $k_page;
            $info['k_rule'] = $k_rule;
            //insert save article_class
            $rs = $this->tmp_model->addKeywords($info);
            if ($rs) {
                $this->redirect('添加关键字成功', PHP_SELF . '?m=keywords');
            } else {
                $this->redirect('添加关键字失败');
            }
        }
    }

}